package com.hifar.test.entrust.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hifar.plat.baseModal.pojo.BaseOssFiles;
import com.hifar.plat.baseModal.service.IBaseOssFilesService;
import com.hifar.sys.MinioUtil;
import com.hifar.test.entrust.constant.EntrustConsts;
import com.hifar.test.entrust.mapper.HfEnvProjectMapper;
import com.hifar.test.entrust.pojo.HfEnvProject;
import com.hifar.test.entrust.pojo.VO.HfEnvProjectSaveVO;
import com.hifar.test.entrust.service.IHfEnvProjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 委托项目 服务实现类
 * </p>
 *
 * @author wei
 * @since 2024-12-04 15:51:56
 */
@Service
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class HfEnvProjectServiceImpl extends ServiceImpl<HfEnvProjectMapper, HfEnvProject> implements IHfEnvProjectService {
    @Autowired
    private IBaseOssFilesService baseOssFilesService;
    @Override
    public void removeByEntrustId(String entrustId) {
        QueryWrapper<HfEnvProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(HfEnvProject::getEntrustId, entrustId);
        this.remove(queryWrapper);
    }

    @Override
    public List<HfEnvProject> listByEntrustId(String entrustId) {
        QueryWrapper<HfEnvProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(HfEnvProject::getEntrustId, entrustId)
                .orderByAsc(HfEnvProject::getRowSort);
        return this.list(queryWrapper);
    }

    @Override
    public List<HfEnvProjectSaveVO> listVOByEntrustId(String entrustId) {
        QueryWrapper<HfEnvProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(HfEnvProject::getEntrustId, entrustId)
                .orderByAsc(HfEnvProject::getRowSort);
        List<HfEnvProject> list = this.list(queryWrapper);
        List<HfEnvProjectSaveVO> voList = new ArrayList<>(list.size());
        for (HfEnvProject hfEnvProject : list) {
            HfEnvProjectSaveVO hfEnvProjectSaveVO = new HfEnvProjectSaveVO();
            BeanUtils.copyProperties(hfEnvProject, hfEnvProjectSaveVO);
            List<BaseOssFiles> testPhoneList = MinioUtil.listByRefId(hfEnvProject.getId(), EntrustConsts.WTD_TEST_PHONE);
            hfEnvProjectSaveVO.setTestPhotoList(testPhoneList);
            voList.add(hfEnvProjectSaveVO);
        }
        return voList;
    }

    @Override
    public List<HfEnvProjectSaveVO> listSubmitVOByEntrustId(String entrustId) {
        QueryWrapper<HfEnvProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
            .eq(HfEnvProject::getEntrustId, entrustId)
            .orderByAsc(HfEnvProject::getRowSort);
        List<HfEnvProject> list = this.list(queryWrapper);
        List<HfEnvProjectSaveVO> voList = new ArrayList<>(list.size());
        for (HfEnvProject hfEnvProject : list) {
            HfEnvProjectSaveVO hfEnvProjectSaveVO = new HfEnvProjectSaveVO();
            BeanUtils.copyProperties(hfEnvProject, hfEnvProjectSaveVO);
            QueryWrapper<BaseOssFiles> fileQueryWrapper = new QueryWrapper<>();
            fileQueryWrapper.lambda()
                .eq(BaseOssFiles::getRefId, hfEnvProject.getId())
                .eq(BaseOssFiles::getRefType, EntrustConsts.WTD_TEST_PHONE)
                .orderByAsc(BaseOssFiles::getRowSort);
            List<BaseOssFiles> testPhoneList = baseOssFilesService.list(fileQueryWrapper);

            hfEnvProjectSaveVO.setTestPhotoList(testPhoneList);
            voList.add(hfEnvProjectSaveVO);
        }
        return voList;
    }
}
